package com.sfzlot.dev.netty.server.client.handler;

import com.sfzlot.dev.netty.bean.Message;
import com.sfzlot.dev.netty.bean.bussiness.Industry;
import com.sfzlot.dev.netty.bean.bussiness.Product;
import com.sfzlot.dev.netty.bean.bussiness.Subdivide;
import com.sfzlot.dev.netty.bean.command.PowerSwitchCommand;
import com.sfzlot.dev.netty.bean.data.DeviceLoginInData;
import com.sfzlot.dev.netty.bean.data.powerswitch.PowerSwitchStatusInData;
import com.sfzlot.dev.netty.bean.identifier.EncryptMethod;
import com.sfzlot.dev.netty.bean.identifier.ResponseIdentifier;
import com.sfzlot.dev.netty.utils.MessageUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * Created by ASUS on 2017/5/27.
 */
public class ClientHandler extends ChannelInboundHandlerAdapter {
	
	final static Logger logger = LoggerFactory.getLogger(ClientHandler.class);

	/**
     * 线路连通时发送消息
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
    	 Runnable implRunnable = new ImplRunnable2(ctx);
         new Thread(implRunnable).start();
    }
    
    class ImplRunnable2 implements Runnable{
        private volatile  int i = 0;
        
        private ChannelHandlerContext ctx;
        
        
        
        public ImplRunnable2(ChannelHandlerContext ctx) {
			super();
			this.ctx = ctx;
		}



		@Override
        public void run() {
			Message message = new Message();
		    message.setIndustry(Industry.SMARTFAMILY);//行业大类
		    message.setSubdivide(Subdivide.SmartFamily.SMARTHOME);//行业细分
	    	message.setVersion(1);
			message.setVin("S0000165E1FD");
			message.setSerial(1);
			message.setEncryptMethod(EncryptMethod.NO_ENCRYPTION);
			
			//智能开关-登录指令-客户端请求体
			message.setProduct(Product.SmartHome.POWERSWITCH);
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_LOGIN);
			message.setResponseIdentifier(ResponseIdentifier.COMMAND);
			DeviceLoginInData deviceLoginInData = new DeviceLoginInData();
			deviceLoginInData.setDataCollectTime(new Date());
			deviceLoginInData.setICCID("1323123123");
			deviceLoginInData.setSerialNum((short)1);
			deviceLoginInData.setDevicePwd("d5c417d106e14cc6a02e6b1b8a03467");
			message.setData(deviceLoginInData);
			String hexString = MessageUtils.getHexString(message);
			System.out.println("智能开关-登录指令-客户端请求体HEX:" + hexString);
			ByteBuf byteBuf = Unpooled.wrappedBuffer(MessageUtils.getBytes(message));
			ctx.writeAndFlush(byteBuf);	
			
			//状态上报
			message.setCommandIdentifier(PowerSwitchCommand.DEVICE_STATUS);
			PowerSwitchStatusInData deviceStatusInData = new PowerSwitchStatusInData();
			// 读取时间
			deviceStatusInData.setDataCollectTime(new Date());
			// 设备状态
			deviceStatusInData.setStatus(1);
			message.setData(deviceStatusInData);
	        while(true) {
	        	try {
					Thread.sleep(30000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        	message.setVersion(message.getVersion());
				hexString = MessageUtils.getHexString(message);
	        	System.out.println("智能开关-状态上报指令-客户端请求体HEX:" + hexString);
	    		byteBuf = Unpooled.wrappedBuffer(MessageUtils.getBytes(message));
	    		ctx.writeAndFlush(byteBuf);	
	        }
            
        }
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    	 Message message = (Message)msg;
         logger.info("解析数据为:{}", message);
    }
    
}
